package com.kedacom.freedomLabel.view.activity.printer;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gprinter.command.LabelCommand;
import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.base.BasicCache;
import com.kedacom.freedomLabel.databinding.ActivityPrinterGpPrinterSettingSelectBinding;
import com.kedacom.freedomLabel.model.annotation.PrinterSettingCategory;
import com.kedacom.freedomLabel.viewmodel.printer.GpPrinterSettingSelectViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.message.LegoMessageManager;

@ContentView(R.layout.activity_printer_gp_printer_setting_select)
@ViewModel(GpPrinterSettingSelectViewModel.class)
public class GpPrinterSettingSelectActivity extends BaseActivity<ActivityPrinterGpPrinterSettingSelectBinding, GpPrinterSettingSelectViewModel> {

    private static final int ITEM_HEIGHT = 60;
    private static final int ITEM_MARGIN = 4;
    TextView mLastSelectedTextView;
    int mSelectPosition = -1;
    @PrinterSettingCategory
    String mSettingCategory;
    private View.OnClickListener mChildrenItemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            beep();
            if (mLastSelectedTextView != null) {
                mLastSelectedTextView.setBackgroundResource(R.drawable.full_line_stroke);
                mLastSelectedTextView.setTextColor(Color.BLACK);
            }
            TextView textView = (TextView) v;
            textView.setBackgroundResource(R.drawable.full_line_stroke_selected);
            textView.setTextColor(Color.WHITE);
            mLastSelectedTextView = textView;
            mSelectPosition = (int) v.getTag(R.integer.tag_position);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSettingCategory = getIntent().getStringExtra(PrinterSettingCategory.class.getSimpleName());
        if (mSettingCategory == null) mSettingCategory = PrinterSettingCategory.DENSITY;
        mViewModel.getSelectItemData(mSettingCategory);
    }

    @OnMessage(value = Options.MSG_GET_GAP, always = true)
    public void receiveSelectGapItemData(String[] data) {
        mBinding.title.setText("标签间隙");
        String[] values = data;
        mBinding.container.removeAllViews();
        addItemView(values);
    }

    @OnMessage(value = Options.MSG_GET_SPEED, always = true)
    public void receiveSelectSpeedItemData(LabelCommand.SPEED[] data) {
        if (data == null) return;
        mBinding.title.setText("打印速度");
        String[] values = new String[data.length];
        int index = 0;
        for (LabelCommand.SPEED speed : data) {
            float value = speed.getValue();
            values[index++] = String.valueOf(value);
        }
        mBinding.container.removeAllViews();
        addItemView(values);
    }

    @OnMessage(value = Options.MSG_GET_DIRECTION, always = true)
    public void receiveSelectDirectionItemData(LabelCommand.DIRECTION[] data) {
        if (data == null) return;
        mBinding.title.setText("打印方向");
        String[] values = new String[data.length];
        int index = 0;
        for (LabelCommand.DIRECTION direction : data) {
            int value = direction.getValue();
            values[index++] = String.valueOf(value);
        }
        mBinding.container.removeAllViews();
        addItemView(values);
    }

    @OnMessage(value = Options.MSG_GET_DENSITY, always = true)
    public void receiveSelectDensityItemData(LabelCommand.DENSITY[] data) {
        if (data == null) return;
        mBinding.title.setText("打印浓度");
        String[] values = new String[data.length];
        int index = 0;
        for (LabelCommand.DENSITY density : data) {
            int value = density.getValue();
            values[index++] = String.valueOf(value);
        }
        mBinding.container.removeAllViews();
        addItemView(values);
    }

    public void onItemUsageButtonClick(View view) {
        beep();
        if (mSelectPosition > -1) {
            String dataKey = Options.MSG_SELECT_ITEM + "_" + mSettingCategory;
            LegoMessageManager.getInstance().sendMessage(dataKey, mSelectPosition);
            // 缓存数据
            BasicCache.getInstance().savePrinterItemIndex(dataKey, mSelectPosition);
            finish();
        }
    }

    public void onBackClick(View view) {
        beep();
        finish();
    }

    private void addItemView(String[] values) {
        LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ITEM_HEIGHT);
        itemParams.topMargin = ITEM_MARGIN / 2;
        itemParams.bottomMargin = ITEM_MARGIN / 2;
        for (int i = 0; i < values.length; i++) {
            String value = values[i];
            TextView textView = new TextView(getContext());
            textView.setLayoutParams(itemParams);
            textView.setTextColor(getResources().getColor(android.R.color.black));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            textView.setGravity(Gravity.CENTER);
            textView.setBackgroundResource(R.drawable.full_line_stroke);
            // 限制显示一行
            textView.setSingleLine(true);
            textView.setMaxLines(1);
            textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            textView.setText(value);
            textView.setTag(value);
            textView.setTag(R.integer.tag_position, i);
            textView.setOnClickListener(mChildrenItemClickListener);
            mBinding.container.addView(textView);
        }
    }
}